package cn.hrlly.tao2shou.biz;

import android.content.Context;

import com.zhy.http.okhttp.OkHttpUtils;

import cn.hrlly.tao2shou.other.Config;
import cn.hrlly.tao2shou.callback.CommodityDetailsCallback;
import cn.hrlly.tao2shou.domain.Commodities;
import cn.hrlly.tao2shou.domain.Commodity;
import cn.hrlly.tao2shou.listener.OnCommodityDetailsListener;
import cn.hrlly.tao2shou.utils.DatabaseUtil;

/**
 * User: gengliang
 * Date: 2016-01-28
 * Time: 19:11
 */

public class CommodityDetailsBizImpl implements CommodityDetailsBiz {

    private OnCommodityDetailsListener onCommodityDetailsListener;
    private DatabaseUtil databaseUtil;

    public CommodityDetailsBizImpl(OnCommodityDetailsListener onCommodityDetailsListener,Context context){
        this.databaseUtil=new DatabaseUtil(context);
        this.onCommodityDetailsListener=onCommodityDetailsListener;
    }

    @Override
    public void showCommodityDetails(Commodity commodity) {
        OkHttpUtils
                .post()
                .url(Config.key_url)
                .addParams(Config.key_action,Config.action_commodity_details)
                .addParams(Config.key_release_id, commodity.getReleaseId())
                .build()
                .execute(new CommodityDetailsCallback(onCommodityDetailsListener));
    }

    @Override
    public void saveHistory(Commodity commodity, String releaseId) {
        Commodities commodities=new Commodities();
        commodities.setReleaseId(releaseId);
        commodities.setCommodityName(commodity.getCommodityName());
        commodities.setCommodityPrice((float) commodity.getExpectedPrice());
        commodities.setCommodityPhoto(commodity.getCommodityPhotos().get(0));
        databaseUtil.insertHistory(commodities);
    }

    @Override
    public void closeDataBase() {
        databaseUtil.closeDatabase();
    }
}
